/*
 *  types.c
 *  Asset modeling
 *
 *  Created by Peeter Meos on 27.02.11.
 *  Copyright 2011 NC3A CAT1. All rights reserved.
 *
 */

#ifndef TYPES_H
	#define TYPES_H
	#include "types.h"
#endif

#ifndef CONST_H
	#define CONST_H
	#include "const.h"
#endif

#ifndef QUEUE_H
	#define QUEUE_H
	#include "queue.h"
#endif

#include <stdlib.h>
#include <string.h>

/* 
 * Stuff for casualties
 */

T_CASUALTY *init_casualty(char type, char location, double	wounding_time)
{
	T_CASUALTY *entity;
	
	entity = (T_CASUALTY *)malloc(sizeof(T_CASUALTY));
	
	entity->type = type;
	entity->location = location;
	
	entity->wounding_time = wounding_time;
	entity->medevac_time = 0;
	entity->role2_time = 0;
	
	entity->next = NULL;
	
	return entity;
}

void free_casualty(T_CASUALTY *entity)
{
	free(entity);
	return;
}

/* 
 * Stuff for events
 */

T_EVENT *init_event(char type, double time, char unit_id)
{
	T_EVENT *event;
	
	event = (T_EVENT *)malloc(sizeof(T_EVENT));
	
	event->type = type;
	event->time = time;
	event->unit_id = unit_id;
	event->next = NULL;
	
	return event;
}

void free_event(T_EVENT *event)
{
	free(event);
	return;
}


/*
 * Stuff for assets
 */

T_ASSET *init_asset(char type, double loc_x, double loc_y, char asset_id)
{
	T_ASSET *asset;
	
	asset = (T_ASSET *)malloc(sizeof(T_ASSET));
	asset->type = type;
	asset->loc_x = loc_x;
	asset->loc_y = loc_y;
	asset->asset_id = asset_id;
	asset->destination = 100;
	
	switch(asset->type)
	{
		case(RW_ASSET):
			asset->range = RW_RANGE;
			asset->speed = RW_SPEED;
			break;
		case(FW_ASSET):
			asset->range = FW_RANGE;
			asset->speed = FW_SPEED;
			break;
		case(AM_ASSET):
			asset->range = AM_RANGE;
			asset->speed = AM_SPEED;
			break;
	}
	
	return asset;
}

void free_asset(T_ASSET *asset)
{
	T_CASUALTY *t;
	
	while (asset->casualties != NULL)
	{
		t = pop_cas(&(asset)->casualties);
		free(t);
	}
	
	free(asset);
	return;
}

T_ASSET *reset_range(T_ASSET *asset)
{
	switch(asset->type)
	{
		case(RW_ASSET):
			asset->range = RW_RANGE;
			break;
		case(FW_ASSET):
			asset->range = FW_RANGE;
			break;
		case(AM_ASSET):
			asset->range = AM_RANGE;
			break;
	}
	
	return asset;
}

/*
 * Stuff for units
 */

T_UNIT *init_unit(double loc_x, double loc_y, int men, char *name, char fw_capable, char unit_id)
{
	T_UNIT *unit;
	
	unit = (T_UNIT *)malloc(sizeof(T_UNIT));
	
	unit->men = men;
	unit->loc_x = loc_x;
	unit->loc_y = loc_y;
	unit->fw_capable = fw_capable;
	unit->unit_id = unit_id;
	unit->num_casualties = 0;
	
	unit->name = malloc(sizeof(char) * 100);
	strncpy(unit->name, name, 100);
	
	return unit;
}

void free_unit(T_UNIT *unit)
{
	T_CASUALTY *t;
	
	while (unit->casualties != NULL)
	{
		t = pop_cas(&(unit)->casualties);
		free(t);
	}
	
	free(unit);
	return;
}